package edu.hpu.demo;


import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;


import java.util.concurrent.Future;

/**
 * @Author 三分恶
 * @Date 2020/7/15
 * @Description 异步方法调用Demo
 */

public class AsyncDeno {

    /**
     * 无返回值的异步方法
     */
    @Async
    public void asyncMethodWithVoidReturnType() {
        System.out.println("异步无返回值方法 "
                + Thread.currentThread().getName());
    }

    /**
     * 有返回值的异步方法
     * @return
     */
    @Async
    public Future<String> asyncMethodWithReturnType() {
        System.out.println("执行有返回值的异步方法 "
                + Thread.currentThread().getName());
        try {
            Thread.sleep(5000);
            return new AsyncResult<String>("hello world !!!!");
        } catch (InterruptedException e) {
            //
        }
        return null;
    }

    @Async("threadPoolTaskExecutor")
    public void asyncMethodWithConfiguredExecutor() {
        System.out.println("Execute method with configured executor - "
                + Thread.currentThread().getName());
    }
}
